var TableManaged = function () {

    var initTable1 = function () {

        var table = $('#sample_1');

        // begin first table
        //table.dataTable({
        // 这块是搜索框与每页数据量
            // Internationalisation. For more info refer to http://datatables.net/manual/i18n
            //"language": {
            //    "aria": {
            //        "sortAscending": ": activate to sort column ascending",
            //        "sortDescending": ": activate to sort column descending"
            //    },
            //    "emptyTable": "No data available in table",
            //    "info": "Showing _START_ to _END_ of _TOTAL_ entries",
            //    "infoEmpty": "No entries found",
            //    "infoFiltered": "(filtered1 from _MAX_ total entries)",
            //    "lengthMenu": "Show _MENU_ entries",
            //    "search": "Search:",
            //    "zeroRecords": "No matching records found"
            //},

            // Or you can use remote translation file
            //"language": {
            //   url: '//cdn.datatables.net/plug-ins/3cfcc339e89/i18n/Portuguese.json'
            //},

            // Uncomment below line("dom" parameter) to fix the dropdown overflow issue in the datatable cells. The default datatable layout
            // setup uses scrollable div(table-scrollable) with overflow:auto to enable vertical scroll(see: assets/global/plugins/datatables/plugins/bootstrap/dataTables.bootstrap.js). 
            // So when dropdowns used the scrollable div should be removed. 
            //"dom": "<'row'<'col-md-6 col-sm-12'l><'col-md-6 col-sm-12'f>r>t<'row'<'col-md-5 col-sm-12'i><'col-md-7 col-sm-12'p>>",

            //"bStateSave": true, // save datatable state(pagination, sort, etc) in cookie.

            //"columns": [{
            //    "orderable": false
            //}, {
            //    "orderable": true
            //}, {
            //    "orderable": false
            //}, {
            //    "orderable": false
            //}, {
            //    "orderable": true
            //}, {
            //    "orderable": false
            //}],
            //"lengthMenu": [
            //    [5, 15, 20, -1],
            //    [5, 15, 20, "All"] // change per page values here
            //],
            // set the initial value
            //"pageLength": 5,
            //"pagingType": "bootstrap_full_number",
            //"language": {
            //    "search": "My search: ",
            //    "lengthMenu": "  _MENU_ records",
            //    "paginate": {
            //        "previous":"Prev",
            //        "next": "Next",
            //        "last": "Last",
            //        "first": "First"
            //    }
            //},
            //"columnDefs": [{  // set default column settings
            //    'orderable': false,
            //    'targets': [0]
            //}, {
            //    "searchable": false,
            //    "targets": [0]
            //}],
            //"order": [
            //    [1, "asc"]
            //] // set first column as a default sort by asc
        //});

        //var tableWrapper = jQuery('#sample_1_wrapper');

        table.find('.group-checkable').change(function () {
            var set = jQuery(this).attr("data-set");
            var checked = jQuery(this).is(":checked");
            jQuery(set).each(function () {
                if (checked) {
                    $(this).attr("checked", true);
                    $(this).parents('tr').addClass("active");
                } else {
                    $(this).attr("checked", false);
                    $(this).parents('tr').removeClass("active");
                }
            });
            jQuery.uniform.update(set);
        });

        //table.on('change', 'tbody tr .checkboxes', function () {
        //    $(this).parents('tr').toggleClass("active");
        //});

        //tableWrapper.find('.dataTables_length select').addClass("form-control input-xsmall input-inline"); // modify table per page dropdown
    };

    var initTable2 = function() {
        $(".table-group-action-submit").click(function(){
            $.ajax({
                type:'POST',
                url:window.location.href,
                data:$('#edit_form').serialize(),
                dataType:'json',
                success: function (jsonData) {
                    if (jsonData.status == true) {
                        Metronic.alert({
                            type: 'success',
                            icon: '',
                            message: jsonData.msg,
                            container: '',
                            place: 'prepend'
                        });
                        setTimeout(function () {
                            window.location.href = jsonData.url;
                        }, 2500);
                    } else {
                        Metronic.alert({
                            type: 'danger',
                            icon: 'warning',
                            message: jsonData.msg,
                            container: '',//grid.getTableWrapper(),
                            place: 'prepend'
                        });
                    }
                },
                error: function (jsonData) {
                    Metronic.alert({
                        type: 'danger',
                        icon: 'warning',
                        message: '异常',
                        container: '',
                        place: 'prepend'
                    });
                }
            });
        });
    };

    var initTable3 = function() {
        //$(".table-group-action-del").click(function () {
        //var url = $('#delData').attr("value");
        $(".delData").click(function () {
            var url = $(this).attr("value");
            $('.table-group-action-del').click(function () {

                $.ajax({
                    type: 'POST',
                    url: url,
                    //data:$('#edit_form').serialize(),
                    dataType: 'json',
                    success: function (jsonData) {
                        if (jsonData.status == true) {
                            Metronic.alert({
                                type: 'success',
                                icon: '',
                                message: jsonData.msg,
                                container: '',
                                place: 'prepend'
                            });
                            setTimeout(function () {
                                window.location.href = jsonData.url;
                            }, 2000);
                        } else {
                            Metronic.alert({
                                type: 'danger',
                                icon: 'warning',
                                message: jsonData.msg,
                                container: '',//grid.getTableWrapper(),
                                place: 'prepend'
                            });
                        }
                    },
                    error: function (jsonData) {
                        Metronic.alert({
                            type: 'danger',
                            icon: 'warning',
                            message: '异常',
                            container: '',
                            place: 'prepend'
                        });
                    }
                });
            });
        });
    }

    return {

        //main function to initiate the module
        init: function () {
            if (!jQuery().dataTable) {
                return;
            }

            initTable1();
            initTable2();
            initTable3();
            $('#back').click(function(){
                history.go(-1);
            });
            $('#reset').click(function(){
                window.location.reload();
            });

        }
    };

}();